//
//  WbNetworking.swift
//  Weibo
//
//  Created by HH on 16/12/3.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case post
    
    case get
}

class WbNetworking: AFHTTPSessionManager {
    
    
    static let sharedTools:WbNetworking = {
    
        let tools = WbNetworking()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        
        return tools
        
    }()
    
    
    //MARK:网络接口
    fileprivate func requestData(type:RequestType,url:String,params:Any?,complete:@escaping (Any?,Error?)->()) {
        
        
        if type == RequestType.post {
            post(url, parameters: params, progress: nil, success: { (_, response) in
                complete(response,nil)
            }, failure: { (_, error) in
                complete(nil,error)
            })
        }else{
            
            get(url, parameters: params, progress: nil, success: { (_, response) in
                complete(response,nil)
            }, failure: { (_, error) in
                complete(nil,error)
            })
        }
    }


}

//Mark:
extension WbNetworking {
    
    //MARK:OAuth2的access_token接口
    func requestAccess_token(code:String,complete:@escaping (Any?,Error?)->()){
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":AppKey,
                      "client_secret":AppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":Redirect_uri]
        
        
       requestData(type: .post, url: url, params: params, complete: complete)
    
    }
    
    
    //MARK:获取用户信息接口
    
    func requestUserInfo(userInfo:WbUserInfoModel,complete:@escaping (Any?,Error?)->()){
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let params = ["access_token":userInfo.access_token!,
                      "uid":userInfo.uid] as [String : Any]
        
        
        requestData(type: .get, url: url, params: params, complete: complete)
        
    }
    
    //获取当前登录用户及其所关注（授权）用户的最新微博
    func requestStatuses_friends_timeline(since_id:Int64,max_id:Int64,complete:@escaping (Any?,Error?)->()){
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params = [
            "access_token":WbUserAccountViewModel.sharedUserAccount.accessToken!,
            "count":30,
            "since_id":since_id,
            "max_id":max_id] as [String : Any]
        
        //打印json地址
        let path = url + "?access_token=" + WbUserAccountViewModel.sharedUserAccount.accessToken!
        print(path)
       
        requestData(type: .get, url: url, params: params, complete: complete)
        
    }
    
    //发布一条新微博
    func updateStatuses(status:String,visible:Int? = 0,lat:CGFloat? = 0.0,long:CGFloat? = 0.0,complete:@escaping (Bool)->()){
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
            "access_token":WbUserAccountViewModel.sharedUserAccount.accessToken,
            "status":status,
            "visible":visible,
            "lat":lat,
            "long":long] as [String:Any?]
        
        requestData(type: .post, url: url, params: params) { (response, error) in
            
            if error != nil{
                
                print(error!)
                complete(false)
            }else{
                complete(true)
            }
            
        }
        
    }
    
    //转发一条微博
    
    func repostStatuses(id:Int64,status:String?,is_comment:Int? = 0,complete:@escaping (Bool)->()){
        
        let url = "https://api.weibo.com/2/statuses/repost.json"
        
        let params = [
            "access_token":WbUserAccountViewModel.sharedUserAccount.accessToken!,
            "id":id,
            "is_comment":is_comment ?? 0] as [String:Any]
        
        
        requestData(type: .post, url: url, params: params) { (response, error) in
            
            if error != nil{
                
                print("转发微博失败")
                print(error!)
                
                complete(false)
            }else{
                print("转发微博成功")
                complete(true)
            }
        }
        
    }
    
}
